import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';

import Home from '../src/views/Home.vue';
import About from '../src/views/About.vue';

const routes = [
    {
        path: "/home",
        name: "home",
        component: Home
    },
    {
        path: "/about",
        component: About,
        children: [
            {
                path: "info",
                component: () => import('../src/views/aboutInfo.vue')
            }
        ]
    }
];

const router = createRouter({
    routes,
    history: createWebHistory()
});

//1.1动态添加路由
const orderRoute = {
    path: "/order",
    component: () => import('../src/views/order.vue')
}
router.addRoute(orderRoute)
//1.2动态添加子路由
const homeMomentRoute = {
    path: 'moment',
    component: () => import('../src/views/homeMoment.vue')
}
router.addRoute('home', homeMomentRoute) //给谁添加子路由写谁的name
 
//2.1动态删除路由
// 添加一个name相同的路由  会删除原来的路由  也叫做替换
//2.2
// router.removeRoute('路由的name')
//2.3添加 路由时会有一个返回值 调用这个返回值就删除
// const removeRoute = router.addRoute(routeRecord)
// removeRoute()

//vue-router4.0 不推荐使用next 
//使用返回值  
//1.false： 不进行导航
//2. undefined或者不写返回值： 进行默认导航
//3.字符串： 路径，跳转到对应的路径中

router.beforeEach((to, from) => {
    // console.log(to)
    // console.log(from)
    // const token = window.localStorage.getItem('token');
    // if (!token) {
    //     return "/login"
    // }
})

export default router
